package com.example.memorypoint.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.bumptech.glide.Glide;
import com.example.memorypoint.R;
import com.kongzue.dialogx.dialogs.PopTip;

import java.net.URL;

public class UserInformationActivity extends AppCompatActivity {

    private EditText mUserName;
    private EditText mUserPhone;
    private Toolbar toolbar;
    private Uri select;
    public static final int GALLERY_REQUEST_CODE = 1001;
    private ImageView imageView;
    private String savedImageUri;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_information);
        ImageView mUserAvatar = findViewById(R.id.img_header_information);
        mUserName = findViewById(R.id.user_name);
        mUserPhone = findViewById(R.id.user_phone);
        toolbar = findViewById(R.id.toolbar);
        imageView = findViewById(R.id.img_header_information);

        mUserAvatar.setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, GALLERY_REQUEST_CODE);
        });

        Button saveChangesBtn = findViewById(R.id.save_changes);
        saveChangesBtn.setOnClickListener(v -> saveChanges());
        toolbarBack();
        initData();
    }

    private void initData() {

        SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);
        savedImageUri = sharedPreferences.getString("user_avatar", null);
        String savedUserName = sharedPreferences.getString("user_name", "");
        String savedUserPhone = sharedPreferences.getString("user_phone", "");

// Set the loaded information to the views
        if (savedImageUri != null) {
            Glide.with(this).load(Uri.parse(savedImageUri)).into(imageView);
            imageView.setTag(Uri.parse(savedImageUri)); // Set the image URI as a tag
        }
        mUserName.setText(savedUserName);
        mUserPhone.setText(savedUserPhone);
    }

    private void toolbarBack() {
        toolbar.setNavigationOnClickListener(view ->
                finish());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GALLERY_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
            Uri selectedImage = data.getData();
            if (selectedImage != null) {
                // Load and display the selected image
                select = selectedImage;
                Log.d("test", "onActivityResult: " + selectedImage);
                imageView.setImageURI(selectedImage);
            }
        }
    }


    private void saveChanges() {
        String newName = mUserName.getText().toString();
        String newPhone = mUserPhone.getText().toString();
        SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if (select != null) {
            editor.putString("user_avatar", select.toString());
        }else if (savedImageUri != null){
            editor.putString("user_avatar", savedImageUri.toString());

        }
        editor.putString("user_name", newName);
        editor.putString("user_phone", newPhone);
        editor.apply();
        PopTip.show("保存成功");
    }

}
